package com.mesd.killer.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mesd.framework.service.impl.BaseServiceImpl;
import com.mesd.framework.spring.ApplicationContextUtil;
import com.mesd.framework.util.DateUtil;
import com.mesd.killer.dao.KillerDao;
import com.mesd.killer.filter.SearchBean;
import com.mesd.killer.model.GameDetail;
import com.mesd.killer.model.GameResult;
import com.mesd.killer.service.KillerService;

@Service("killerService")
@Transactional(propagation = Propagation.SUPPORTS)
public class killerServiceImpl extends BaseServiceImpl implements KillerService
{

	@Autowired
	KillerDao killerDao;

	private static final double PERCENT = 0.9;

	@Override
	public List<GameResult> getAllGameResults()
	{

		return null;
	}

	@Override
	public List<GameResult> getGameResults()
	{
		return null;
	}

	@Override
	public List<GameResult> SearchGameResult(SearchBean searchBean)
	{
		List<Long> gameIds = killerDao.searchGameIds(searchBean);
		return killerDao.getGameResults(gameIds);
	}

	@Override
	public List<GameResult> searchTodayGameResult()
	{
		return killerDao.getTodayGameResults();
	}

	@Override
	public List<GameResult> searchGameResult(Date start, Date end)
	{
		List<GameResult> gameResults = killerDao.getGameResults(
			start, end);
		return gameResults;
	}

	public List<GameResult> searchCurrentGameResult()
	{
		List<GameResult> gameResults = killerDao.getCurrentGameResult();
		return gameResults;
	}

	@Override
	public GameResult getGameResultByGameId(long gameId)
	{
		return (GameResult) killerDao.getObject(
			GameResult.class, GameResult.GAME_ID, gameId);
	}

	@Override
	public void saveGameResult(GameResult gameResult)
	{
		killerDao.saveObject(gameResult);

	}

	@Override
	public void saveGameDetail(GameDetail gameDetail)
	{
		killerDao.saveObject(gameDetail);

	}

	@Override
	public List<GameResult> SearchGameResultByAll(SearchBean searchBean)
	{
		List<Long> gameIds = killerDao.searchGameIdsByAll(searchBean);
		return killerDao.getGameResults(gameIds);
	}

	@Override
	public String getAnalysisResult(List<GameResult> gameResults, boolean sendMail)
	{
		int winCount = 0;
		int daCount = 0;
		int lostCount = 0;
		int xiaoCount = 0;
		int draw1 = 0;
		int draw2 = 0;
		int halfDaxiaoCount = 0;
		int halfXiao = 0;
		int xiaDaxiaoCount = 0;
		int halfHostWinCount = 0;
		int halfDrawCount = 0;
		int halfVisitWinCount = 0;
		int fullHostWinCount = 0;
		int fullDrawCount = 0;
		int fullVisitWinCount = 0;
		String killerResult = "";
		if (gameResults != null)
		{
			int count = gameResults.size();
			if (count > 0)
			{
				for (GameResult gr : gameResults)
				{
					String halfScore = gr.getHalfScore();
					String scores[] = halfScore.split("-");
					if (scores.length < 2)
					{
						System.out.println(gr.getGameId());
						continue;
					}
					int hh = 0;
					if (scores[0].trim().length() != 0)
					{
						hh = Integer.parseInt(scores[0].trim());
					}
					int vh = 0;
					if (scores[1].trim().length() != 0)
					{
						vh = Integer.parseInt(scores[1].trim());
					}
					int halfDaxiao = hh + vh;
					if (halfDaxiao > 0)
					{
						halfDaxiaoCount++;
					}
					if (halfDaxiao == 0)
					{
						halfXiao++;
					}
					if (hh > vh)
					{
						halfHostWinCount++;
					}
					if (hh == vh)
					{
						halfDrawCount++;
					}
					if (hh < vh)
					{
						halfVisitWinCount++;
					}
					String fullScore = gr.getFullScore();
					String fullScores[] = fullScore.split("-");
					if (fullScores.length < 2)
					{
						System.out.println(gr.getGameId());
						continue;
					}
					int hf = 0;
					if (fullScores[0].trim().length() != 0)
					{
						hf = Integer.parseInt(fullScores[0].trim());
					}
					int vf = 0;
					if (fullScores[1].trim().length() != 0)
					{
						vf = Integer.parseInt(fullScores[1].trim());
					}
					int fullDaxiao = hf + vf;
					if (fullDaxiao - halfDaxiao > 0)
					{
						xiaDaxiaoCount++;
					}
					if (hf > vf)
					{
						fullHostWinCount++;
					}
					if (hf == vf)
					{
						fullDrawCount++;
					}
					if (hf < vf)
					{
						fullVisitWinCount++;
					}
					if (gr.getPankouResult() != null && gr.getPankouResult().trim().equals(
						"赢"))
					{
						winCount++;
					}
					if (gr.getPankouResult() != null && gr.getPankouResult().trim().equals(
						"输"))
					{
						lostCount++;
					}
					if (gr.getPankouResult() != null && gr.getPankouResult().trim().equals(
						"走"))
					{
						draw1++;
					}
					if (gr.getDaxiaoResult() != null && gr.getDaxiaoResult().trim().equals(
						"大"))
					{
						daCount++;
					}
					if (gr.getDaxiaoResult() != null && gr.getDaxiaoResult().trim().equals(
						"小"))
					{
						xiaoCount++;
					}
					if (gr.getDaxiaoResult() != null && gr.getDaxiaoResult().trim().equals(
						"走"))
					{
						draw2++;
					}
				}
				double dcount = (double) count;
				killerResult = "共" + count + "场 ,全大" + daCount + ",半大" + halfDaxiaoCount + ",下半大"
					+ xiaDaxiaoCount;
				if (count > 1
					&& (count == halfDaxiaoCount || count == xiaDaxiaoCount || count == daCount))
				{
					killerResult = "<font color='#ff2121'>" + killerResult + "</font>";
				}
				else if (dcount > 4
					&& ((daCount > 0 && daCount / dcount >= PERCENT)
						|| (xiaDaxiaoCount > 0 && xiaDaxiaoCount / dcount >= PERCENT) || (halfDaxiaoCount > 0 && halfDaxiaoCount
						/ dcount >= PERCENT)))
				{
					killerResult = "<font color='#ff0097'>" + killerResult + "</font>";
				}
				else if (sendMail)
				{
					killerResult = "";
				}
			}

		}
		return killerResult;
	}

	@Override
	public List<GameResult> searchDaqiuGameResult()
	{
		return killerDao.getDaqiuGameResult();
	}

}
